package com.zhenai.rocyuan.commons.uitls;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;

public class ServletUtils {
	protected ServletUtils() {
	}

	public static boolean isGet(HttpServletRequest request) {
		return "get".equalsIgnoreCase(request.getMethod());
	}

	public static boolean isPost(HttpServletRequest request) {
		return "post".equalsIgnoreCase(request.getMethod());
	}

	public static boolean isPut(HttpServletRequest request) {
		return "put".equalsIgnoreCase(request.getMethod());
	}

	public static boolean isAjax(HttpServletRequest request) {
		String requestWith = request.getHeader("X-Requested-With");
		return "XMLHttpRequest".equalsIgnoreCase(requestWith);
	}

	public static String getRealIp(HttpServletRequest request) {
		final String cdnIp = request.getHeader("Cdn-Src-Ip");
		if (StringUtils.isNotEmpty(cdnIp)) {
			return cdnIp;
		}
		final String forwardIp = request.getHeader("HTTP_X_FORWARDED_FOR");
		if (StringUtils.isNotEmpty(forwardIp)) {
			return forwardIp;
		}
		final String realIp = request.getHeader("HTTP_X_REAL_IP");
		if (StringUtils.isNotEmpty(realIp)) {
			return realIp;
		} else {
			return request.getRemoteAddr();
		}
	}

}
